<?php

$url = $_POST['url'] ?? '';
if (empty($url)) {
    echo '<form method="POST">
            <input type="text" name="url" placeholder="Enter URL" required>
            <button type="submit">Submit</button>
          </form>
          <a href="https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=mGUNr7Hn0POceOLw6hvoks9M&redirect_uri=oob&scope=basic,netdisk&display=mobile&confirm_login=1" rel="noreferrer" target="_blank">https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=mGUNr7Hn0POceOLw6hvoks9M&redirect_uri=oob&scope=basic,netdisk&display=mobile&confirm_login=1</a>';
    exit;
}

$parsedUrl = parse_url($url);
$hash = $parsedUrl['fragment'] ?? ''; // fragment 就是 # 后面的部分
parse_str($hash, $queryParams);
$accessToken = $queryParams['access_token'] ?? null;

$content = json_encode([
    "token" => $accessToken,
    "entrancepath" => "/story"
], JSON_UNESCAPED_SLASHES);

function base64_urlSafeEncode($data)
{
    return rtrim(strtr(base64_encode($data), '+/', '-_'));
}

$ak = '6pkzPF9YtxgK86hqeHwykmbBU2lduedvJOkp8sWS';
$sk = 'U0uXttcLBytW8CAp7k_sO_SYtDRaYRlagewF4asg';

$putPolicy = [
    "scope" => 'distribution:tingshu/config.json',
    "deadline" => time() + 3600,
    'insertOnly' => 0,
];

$putPolicyJson = json_encode($putPolicy);
$putPolicyJsonBase64 = base64_urlSafeEncode($putPolicyJson);

$signature = hash_hmac('sha1', $putPolicyJsonBase64, $sk, true);
$signatureBase64 = base64_urlSafeEncode($signature);

$uploadToken = $ak . ':' . $signatureBase64 . ':' . $putPolicyJsonBase64;

// 文件上传
$url = 'http://up-z0.qiniup.com';

$postFields = [
    'token' => $uploadToken, // 上传凭证
    'file' => $content, // 文件名
    'key' => 'tingshu/config.json', // 文件存储的名称
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
    exit;
}
curl_close($ch);

$response = json_decode($response);
echo $response->key . "<br />";


// 刷新cdn
$url = 'http://fusion.qiniuapi.com/v2/tune/refresh';

$postFields = [
    "urls" => ['http://dist.360cbs.com/tingshu/config.json']
];
$postFields = json_encode($postFields);

$signOrigin = "POST /v2/tune/refresh";
$signOrigin .= "\nHost: fusion.qiniuapi.com";
$signOrigin .= "\nContent-Type: application/json";
$signOrigin .= "\n\n" . $postFields;
$signature = hash_hmac('sha1', $signOrigin, $sk, true);
$signatureBase64 = base64_urlSafeEncode($signature);
$token = $ak . ':' . $signatureBase64;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Qiniu ' . $token,
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
    exit;
}
curl_close($ch);

$response = json_decode($response);
echo $response->code . "<br />";